package com.songlanyun.modules.publicbenefitmsg.controller.api;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.publicbenefitmsg.entity.PublicBenefitMsgEntity;
import com.songlanyun.modules.publicbenefitmsg.service.PublicBenefitMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 公益记录
 *
 * @author wuchao
 * @email songlanyun@gmail.com
 * @date 2022-08-05 11:06:31
 */
@RestController
@RequestMapping("/api/donation/msg")
public class PublicBenefitMsgApiController extends AbstractApiController {
    @Autowired
    private PublicBenefitMsgService publicBenefitMsgService;

    /**
     * 列表
     */
    @GetMapping("/page")
    //@RequiresPermissions(":donationrecord:list")
    public R page(@RequestParam Map<String, Object> params) {
        PageUtils page = publicBenefitMsgService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions(":donationrecord:list")
    public R list(@RequestParam Map<String, Object> params) {
        Long userId = MapUtil.getLong(params, "userId");
        Integer type = MapUtil.getInt(params, "type");
        Integer limit = MapUtil.getInt(params, "limit");

        List<PublicBenefitMsgEntity> list = publicBenefitMsgService.lambdaQuery()
                .eq(ObjectUtil.isNotNull(userId), PublicBenefitMsgEntity::getUserId, userId)
                .eq(ObjectUtil.isNotNull(type), PublicBenefitMsgEntity::getType, type)
                .last(ObjectUtil.isNotNull(limit), "limit " + limit)
                .orderByDesc(PublicBenefitMsgEntity::getCreateTime)
                .list();

        //加载消息体
        list.forEach(PublicBenefitMsgEntity::loadMsgBody);

        return R.ok().put("list", list);
    }

}
